<?php

function getStoreFile($date)
{
    return 'fetch/'.date("Y-n-j", $date).".html";
}

function getStoreFile2($date)
{
    return 'fetch/'.date("Y", $date).'/'.date("Y-n-j", $date).".html";
}


function isfetched($date)
{
    $fn2=getStoreFile2($date);
    if(file_exists($fn2))
        return true;
    
    $fn=getStoreFile($date);
    if(file_exists($fn))
    {
        rename($fn, $fn2);
        return true;
    }
    return false;
}

//http://nongli.911cha.com/2012-2-7.html
function getURL($date)
{
    return 'http://nongli.911cha.com/'.date("Y-n-j", $date).".html";
}

function  processDate($date)
{
    if(isfetched($date))
        return true;
    $url=getURL($date);
    $context = stream_context_create(array("http"=>array(
            "method" => "POST",
            "header" => "Accept: xml/*, text/*, */*\r\n",
            "ignore_errors" => false,
            "timeout" => 50,
    )));
    ini_set("user_agent" , "Mozilla/5.0\r\nAccept: */*\r\nX-Padding: Foo");
    $page= file_get_contents($url, false, $context, 0);

    if($page==false)
        return false;
    if(strlen($page)<2000)
        exit();
    $fn=getStoreFile2($date);
    file_put_contents($fn,$page);
    echo date("Y-n-j", $date).'  processed'. PHP_EOL;
    sleep(rand(1,5));
    return true;
}


function processDays($startDate,$count)
{
    $step=60*60*24;
    $date=$startDate;
    echo "In oricess".date('Y',$startDate). PHP_EOL;
    for($i=0;$i<$count;$i++)
    {
        if(processDate($date)==false)
        {
            echo "false";
            break;
        }
        else
        {

        }
        $date+=$step;
         
    }

}

$date=strtotime('2020-01-01');
$dateF=$date;

for($i=0;$i<121;$i++)
{
    echo "Year:".date('Y',$dateF).PHP_EOL;
    processDays($dateF,366);
    $dateF=strtotime(date('Y-n-j',$dateF)."-1 year");
}

echo "END";
//$date2=strtotime(date('Y-n-j',$date)."+1 year");

//echo date('Y-n-j',$date2);

//$dateOneYearAdded = strtotime(date("Y-m-d", strtotime($currentDate)) . " +1 year");

/*
 for($y=0;$y<112;$y++)
 {
$startDate=strtotime(2012-$y.'-1-1');
echo date('Y-n-j',$startDate). PHP_EOL;
$date=$startDate;
$ss="";
for($i=0;$i<366;$i++)
{
if(isfetched($date))
{

}else
{
$url=getURL($date);
$s='<a href="'.$url.'">'.date("Y-n-j", $date).".html</a></br>\n";
$ss.=$s;
}
$date=strtotime(date('Y-n-j',$date)."+1 day");
}

if(strlen($ss)==0)
{

}else
{
$pagedata="<html><body></br>";
$pagedata.=$ss;
$pagedata.="</body></html>";
$fn=date('Y',$startDate).'.html';
file_put_contents($fn,$pagedata);
}
}
*/
